<template>
  <div class="dashboard-container" style="padding: 200px 30px 445px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

    <!-- 科目名称 -->
    <el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <div class="grid-content bg-purple">
            <el-form-item label="科目名称：" prop="subject_name" style="width: 100%;">
              <el-input v-model="ruleForm.subject_name" placeholder="请输入科目名称"></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-row>
    <!-- 科目分类 -->
    <el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6" class="Subject-classification">
          <el-form-item label="科目分类：" prop="subject_catid" style="width: 100%;">
            <div class="grid-content bg-purple">
              <el-select @change="queryTwoLevel(ruleForm.subject_parentid)" v-model="ruleForm.subject_parentid" placeholder="一级分类">
                <el-option v-for="item in oneLevel" :key="item.id" :label="item.cat_name" :value="item.id"></el-option>
              </el-select>
            </div>
            <div class="grid-content bg-purple-light">
              <el-select @change="queryThreeLevel(ruleForm.subject_secondid)" v-model="ruleForm.subject_secondid" placeholder="二级分类">
               <el-option v-for="item in twoLevel" :key="item.id" :label="item.cat_name" :value="item.id"></el-option>
              </el-select>
            </div>
            <div class="grid-content bg-purple">
              <el-select v-model="ruleForm.subject_catid" placeholder="三级分类">
                <el-option v-for="item in threeLevel" :key="item.id" :label="item.cat_name" :value="item.id"></el-option>
              </el-select>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-row>
    <!-- 修改 -->
    <el-row class="add-to1">
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <div class="add-to1-div">
           <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
          </div>
        </el-col>
      </el-row>
    </el-row>
    </el-form>
  </div>
</template>

<script>
  import {cateComList} from "@/api/menu";
 import {subjectEdit, subjectInfo} from '@/api/Subject_management'
 import {getInfo} from "@/api/user";
 import {mapGetters} from "vuex";
export default {
  data() {
    return {
      input: '',
      group_title: '',
      value: '',
      ruleForm: {
        subject_name: '',
        subject_parentid: '',
        subject_secondid: '',
        subject_catid: ''
      },
      rules: {
        subject_name: [
          { required: true, message: '请输入科目名称', trigger: 'blur' }
        ],
        subject_parentid: [
           { required: true, message: '请选择一级分类', trigger: 'blur' }
        ],
        subject_secondid: [
          { required: true, message: '请选择二级分类', trigger: 'blur' }
        ],
        subject_catid: [
          { required: true, message: '请选择三级分类', trigger: 'blur' }
        ]
      },
      oneLevel: [],
      twoLevel: [],
      threeLevel: [],
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ]),
  },
  mounted() {
    this.getOneLevel()
    this.getInform()
  },
  filters: {

  },
  methods: {
    getInform(){
      subjectInfo(this.$route.query.id).then(response => {
        this.ruleForm = response.data;
        this.queryTwoLevel(response.data.subject_parentid)
        this.queryThreeLevel(response.data.subject_secondid)
      });
    },
   /**
    * 提交
    * @param formName
    */
   submitForm(formName) {
     this.$refs[ formName ].validate((valid) => {
       if ( valid ) {
         this.listLoading = true;
         subjectEdit({
           "id": this.$route.query.id,
            "subject_name": this.ruleForm.subject_name,
            "subject_parentid":this.ruleForm.subject_parentid ,
            "subject_secondid": this.ruleForm.subject_secondid,
            "subject_catid": this.ruleForm.subject_catid
          }).then(response => {
           this.listLoading = false;
           console.log(response);
           this.$message({message: '修改成功', type: 'success', duration: 1000});
           this.$router.push({path: '/projectManagerList'})
         });
       } else {
         console.log('error submit!!');
         return false;
       }
     });
   },
    /**
     * 查询二级数据
     */
    queryTwoLevel(value) {
      console.log(value);
      this.ruleForm.secondid = '';
      this.ruleForm.catid = '';
      cateComList({
        cate_parentid: value,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.twoLevel = response.data
        this.twoLevel.unshift({id: "", cat_name: "请选择"})
      })
    },
    /**
     * 查询三级数据
     */
    queryThreeLevel(value) {
      this.ruleForm.catid = '';
      console.log(value);
      cateComList({
        cate_parentid: value,
        cate_secondid: this.ruleForm.parentid
      }).then(response => {
        console.log(response);
        this.threeLevel = response.data
        this.threeLevel.unshift({id: "", cat_name: "请选择"})
      })
    },
    /**
     * 获取一级数据
     */
    getOneLevel() {
      cateComList({
        cate_parentid: 0,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.oneLevel = response.data
        this.oneLevel.unshift({id: "", cat_name: "请选择"})
      })
    }
  }
}
</script>
<style scoped>

</style>
<style>
  /* 科目名称开始 */
  .grid-content,.Subject-classification{display: flex;}
  .grid-content .Subjectname,.Subject-classification-span{width: 13%; display: flex; align-items: center;}
  /* 科目名称结束 */
  /* 科目分类开始 */
  .Subject-classification .el-form-item__content{display: flex;}
  .Subject-classification .Subject-classification-span{width: 39.5%;}
  .Subject-classification .bg-purple-light{margin: 0px 10px;}
  .Subject-classification .grid-content,.Subject-classification .el-select{width: 100%;}
  /* 科目分类结束 */
  /* 添加开始 */
  .add-to1 .bg-purple{align-items: center;justify-content: center;}
  .add-to1 .el-button{width: 100%;}
  .add-to1 .add-to1-div{margin-left: 100px;}
  /* 添加结束 */
</style>
